package edu.gatech.wms.views;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.ListFragment;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import edu.gatech.wms.R;
import edu.gatech.wms.controllers.ItemManager;
import edu.gatech.wms.controllers.Login;
import edu.gatech.wms.controllers.UserManager;

//import edu.gatech.wms.views.LoginActivity.UserLoginTask;


public class LandingPageFragment extends ListFragment
{
	private List<Integer>			ids;
	private List<String>			names;
	private ArrayAdapter<String>	adapter;
	private SparseIntArray			pos;
	private int						myId;
	private TextView 				warning;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View view = inflater.inflate(R.layout.landing_page, container, false);
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
		TextView name = (TextView) view.findViewById(R.id.landing_page_name);
		TextView contact = (TextView) view.findViewById(R.id.landing_page_contact);
		warning = (TextView) view.findViewById(R.id.landing_page_warning);
		name.setText(UserManager.getUserFullName(prefs.getInt(Login.ACTIVE_KEY, 0)));
		contact.setText(UserManager.getUserContact(prefs.getInt(Login.ACTIVE_KEY, 0)));
		
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
		myId = preferences.getInt(Login.ACTIVE_KEY, 0);
		
		ids = UserManager.getUserOwnedItems(myId);
		names = new ArrayList<String>();
		pos = new SparseIntArray();

		for (int id : ids)
		{
			pos.put(names.size(), id);
			names.add(ItemManager.getItemName(id));
		}
		adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, names);
		setListAdapter(adapter);
		
		if(!adapter.isEmpty())
			warning.setVisibility(View.GONE);
		else
			warning.setVisibility(View.VISIBLE);
		
		view.findViewById(R.id.add_item).setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(getActivity(), CreateItemActivity.class);
				getActivity().startActivityForResult(intent,1);
			}
		});
		
		view.findViewById(R.id.Logout).setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(getActivity(), LoginActivity.class);
				
				Login.logout();
				getActivity().startActivityForResult(intent,1);
			}
		});
		
		
		return view;
	}

	@Override
	public void onResume()
	{
		super.onResume();

		if(!adapter.isEmpty())
			warning.setVisibility(View.GONE);
		else
			warning.setVisibility(View.VISIBLE);
		
		ids = UserManager.getUserOwnedItems(myId);
		names = new ArrayList<String>();
		pos = new SparseIntArray();
		
		for (int id : ids)
		{
			pos.put(names.size(), id);
			String name = ItemManager.getItemName(id);  
			names.add(name);
		}
		
		adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,
				names);
		setListAdapter(adapter);
	}


	public void onListItemClick(ListView l, View v, int position, long id)
	{
		Intent intent = new Intent(getActivity(), EditItemActivity.class);
		intent.putExtra(Login.ITEM_KEY, pos.get(position));
		getActivity().startActivityForResult(intent, 1);
	}
	
	@Override
	public void onAttach(Activity activity)
	{
		super.onAttach(activity);

	}
}